package com.frame.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.frame.Page;
import com.frame.domain.Role;
import com.frame.domain.RolePermission;
import com.mini.core.PageResult;
import com.mini.core.Record;
import com.mini.core.dao.MiniDao;

/**
 * @author sxjun
 * @version 2016-6-02
 */
@Service
public class RoleService{

	@Autowired
	MiniDao miniDao;
	
	/**
	 * 查找所有角色
	 * @return
	 */
	public List<Role> findAll(){
		String sql = "SELECT * FROM ROLE";
		return miniDao.findList(sql, Record.class);
	}
	
	/**
	 * 分页查询用户
	 * @param page
	 * @param treeId
	 * @return
	 */
	public Page<Role> paginateResult(Page<Role> page,String treeId){
		String sql = "SELECT * FROM ROLE WHERE 1=1";
		PageResult<Role> roles = miniDao.paginateResult(sql, page.getPageNo(), page.getPageSize(), Role.class);
		page.setPageResult(roles);
		return page;
	}

	/**
	 * 插入数据
	 * @param role
	 */
	public void save(Role role) {
		miniDao.insert(role);
	}
	
	/**
	 * 更新数据
	 * @param role
	 */
	public void update(Role role) {
		miniDao.update(role);
	}
	
	/**
	 * 根据Id删除角色
	 * @param id
	 */
	public void delete(String id){
		miniDao.deleteById(Role.class, id);
	}

	/**
	 * 获取角色
	 * @param id
	 * @return
	 */
	public Role findById(String id) {
		return miniDao.findById(Role.class, id);
	}

	/**
	 * 通过roleid删除rolepermission
	 * @param roleid
	 */
	public void deleteRolePermissonByRoleid(String roleid) {
		miniDao.execute("DELETE FROM ROLE_PERMISSION WHERE ROLEID=?", roleid);
	}

	/**
	 * 保存RolePermission
	 * @param roleperm
	 */
	public void saveRolePermisson(RolePermission roleperm) {
		miniDao.insert(roleperm);
	}

	/**
	 * 根据userid获取对应的角色
	 * @param id
	 * @return
	 */
	public List<Role> findByUserid(String userid) {
		return miniDao.findList("SELECT r.* FROM ROLE r,USER_ROLE ur WHERE r.ID=ur.ROLEID AND ur.USERID=?", Role.class, userid);
	}

}
